package org.jeecg.modules.flow.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.activiti.engine.task.TaskInfo;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.SecurityUtils;
import org.jeecg.common.constant.CacheConstant;
import org.jeecg.common.exception.JeecgBootException;
import org.jeecg.common.system.vo.LoginUser;
import org.jeecg.modules.flow.entity.FlowAudit;
import org.jeecg.modules.flow.mapper.FlowAuditMapper;
import org.jeecg.modules.flow.query.SubmitTaskQuery;
import org.jeecg.modules.flow.service.IFlowAuditService;
import org.jeecg.modules.flow.vo.FlowActAuditVo;
import org.springframework.beans.BeanUtils;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;

/**
 * @Description: flow_audit
 * @Author: zzq
 * @Date: 2021-11-08
 * @Version: V1.0
 */
@Service
public class FlowAuditServiceImpl extends ServiceImpl<FlowAuditMapper, FlowAudit> implements IFlowAuditService {

    @Override
    @Cacheable(cacheNames = CacheConstant.FLOW_AUDIT_CACHES, key = "#processInstanceId")
    public List<FlowAudit> listByInstanceId(String processInstanceId) {
        List<FlowAudit> list = lambdaQuery().eq(FlowAudit::getProcessInstanceId, processInstanceId).list();
        return list;
    }


    @Override
    @Cacheable(cacheNames = CacheConstant.FLOW_AUDIT_CACHE, key = "#taskId")
    public FlowAudit getByTaskId(String taskId) {
        FlowAudit one = lambdaQuery().eq(FlowAudit::getTaskId, taskId).one();
        return one;
    }

    @Override
    @CacheEvict(cacheNames = CacheConstant.FLOW_AUDIT_CACHES, key = "#flowAudit.processInstanceId")
    public Integer addAudit(FlowAudit flowAudit) {
        if (flowAudit == null || StringUtils.isBlank(flowAudit.getProcessInstanceId())) {
            throw new JeecgBootException("流程实例ID不能为空");
        }
        return baseMapper.insert(flowAudit);
    }

    @Override
    @CacheEvict(cacheNames = CacheConstant.FLOW_AUDIT_CACHES, key = "#task.processInstanceId")
    public boolean saveAudit(TaskInfo task, SubmitTaskQuery query, LoginUser user) {
        // 填写审核表
        FlowAudit audit = new FlowAudit();
        audit.setAuditInfo(query.getComment());
        audit.setTaskId(task.getId());
        audit.setProcessInstanceId(task.getProcessInstanceId());
        audit.setAuditUserId(user.getId());
        audit.setAuditUserName(user.getRealname());
        audit.setOrgId(user.getOrganizationId());
        audit.setBusinessKey(query.getBusinessKey());
        audit.setOperationType(query.getSubmitType());
        return save(audit);
    }

    @Override
    public List<FlowActAuditVo> getByTaskIds(Set<String> taskIds) {

        List<FlowAudit> list = lambdaQuery().select(FlowAudit::getTaskId, FlowAudit::getOperationType, FlowAudit::getAuditInfo)
                .in(FlowAudit::getTaskId, taskIds).list();
        List<FlowActAuditVo> vos = new ArrayList<>();
        for (FlowAudit flowAudit : list) {
            FlowActAuditVo temp = new FlowActAuditVo();
            BeanUtils.copyProperties(flowAudit, temp);
            vos.add(temp);
        }
        return vos;
    }
}
